/*
 * JSF Toolkit Component Framework
 * Copyright (C) 2007 Noah Sloan <iamnoah A-T gmail D0T com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 */
package com.jsftoolkit.ajax;

import javax.faces.context.FacesContext;

/**
 * This implementation only detects Apache Trinidad. Otherwise it returns a
 * {@link NoServerAjaxContext}. For platforms without a server side component,
 * e.g. DynaFaces (JSF Extensions), no server side context is necessary.
 * 
 * @author noah
 * 
 */
public class DefaultAjaxContextFactory implements AjaxContextFactory {

	private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
			.getLog(DefaultAjaxContextFactory.class);

	private boolean trinidadAvailable;

	private boolean a4jAvailable;

	public DefaultAjaxContextFactory() {
		try {
			// see if Trinidad is loaded
			org.apache.myfaces.trinidad.context.RequestContext.class.getName();
			setTrinidadAvailable(true);
		} catch (NoClassDefFoundError e0) {
			// ignore
			LOG.debug("Apache Trinidad PPR is not available.");
		}
	}

	public AjaxContext create(FacesContext context) {
		if (trinidadAvailable) {
			return new TrinidadAjaxContext();
		} else if (a4jAvailable) {
		}
		// other frameworks (e.g. DynaFaces) do not have a server side component
		return new NoServerAjaxContext();
	}

	public boolean isA4jAvailable() {
		return a4jAvailable;
	}

	public void setA4jAvailable(boolean available) {
		a4jAvailable = available;
	}

	public boolean isTrinidadAvailable() {
		return trinidadAvailable;
	}

	public void setTrinidadAvailable(boolean trinidadAvailable) {
		this.trinidadAvailable = trinidadAvailable;
	}

}
